<?php

function mentions_get_statuses($before,$after,$user_id) {
	$where = '';
	$sort = 'DESC';
	if ($before != -1) {
		$sort = 'DESC';
		$where = ' AND stat_id < '.$before.' ';
	} elseif ($after != -1) {
		$sort = 'ASC';
		$where = ' AND stat_id > '.$after.' ';
	}
	$statuses = db_array('SELECT stat_id,stat_date,stat_txt,reply_stat,restat_from,user_id,username,name FROM users INNER JOIN (SELECT status.* FROM status INNER JOIN mentions ON stat_id = mention_stat WHERE mention_who = '.$user_id.' '.$where.' ORDER BY stat_id '.$sort.' LIMIT 0,11) AS statuses ON user_id = stat_user'); // equal 11 okay to more
	if ($sort == 'ASC') {
		$statuses = array_reverse($statuses);
	}
	return extend_statuses($statuses);
}

if (defined('NO_DIRECT')) {
	if (isset($_GET['i'])) { // id orang itu
		$user = false;
		if ($session['user']['user_id'] == $_GET['i']) {
			$user = $session['user'];
		} else {
			$user = db_result('SELECT * FROM users WHERE user_id = '.intval($_GET['i']));
		}
		if ($user === false) {
			// user not found
			echo 'user not found';
		} else {
			if (isset($_GET['before'])) {
				$statuses = mentions_get_statuses(intval($_GET['before']),-1,intval($_GET['i']));
			} elseif (isset($_GET['after'])) {
				$statuses = mentions_get_statuses(-1,intval($_GET['after']),intval($_GET['i']));
			} else {
				$statuses = mentions_get_statuses(-1,-1,intval($_GET['i']));
			}
			if (isset($_GET['ajax'])) {
				header('Content-type: text/plain');
			} else {
				$post = get_posts($user['user_id']);
				$following = get_followings($user['user_id']);
				$follower = get_followers($user['user_id']);
				if ($session['user']['user_id'] == $_GET['i']) {
					$his_post = $post;
					$his_following = $following;
					$his_follower = $follower;
				} else {
					$his_post = get_posts(intval($_GET['i']));
					$his_following = get_followings(intval($_GET['i']));
					$his_follower = get_followers(intval($_GET['i']));
				}
				foreach ($statuses as $a) {
					echo '<p><strong>@'.$a['username'].'</strong><br />'.show_status($a['stat_txt']).'<br />'.$a['stat_date'].'</p>';
				}
			}
		}
	}
}
